package com.qyhl.module_practice.rank.person;


import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeIsVolunteerBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerDetailBean;

import java.util.List;

public class PracticeScoreRankModel implements PracticeScoreRankContract.PracticeScoreRankModel {
    private PracticeScoreRankPresenter mPresenter;

    PracticeScoreRankModel(PracticeScoreRankPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }


    @Override
    public void getList(String page, int type) {
        String url = "";
        switch (type) {
            case 1:
                url = PracticeUrl.GET_VOLUNTEER_LIST_BY_TIME;
                break;
            case 2:
                url = PracticeUrl.GET_VOLUNTEER_LIST_BY_ACT;
                break;
            case 3:
                url = PracticeUrl.GET_PRACTICE_SCORE_PERSONAL_RANK;
                break;
        }
        EasyHttp.get(url)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeVolunteerBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何志愿者！", !page.equals("1"));
                        } else {
                            mPresenter.setError("获取列表失败，请重新尝试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeVolunteerBean> practiceVolunteerBeans) {
                        if (practiceVolunteerBeans != null && practiceVolunteerBeans.size() > 0) {
                            mPresenter.setList(practiceVolunteerBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError("暂无任何志愿者！", !page.equals("1"));
                        }
                    }
                });
    }

    @Override
    public void getDetail(String volId) {
        EasyHttp.get(PracticeUrl.GET_PRACTICE_VOL_SIMPLE_INFO)
                .params("volId", volId)
                .execute(new SimpleCallBack<PracticeVolunteerDetailBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setDetail(null);
                    }

                    @Override
                    public void onSuccess(PracticeVolunteerDetailBean practiceVolunteerDetailBean) {
                        mPresenter.setDetail(practiceVolunteerDetailBean);
                    }
                });
    }

    @Override
    public void getIsVolunteer(String phone) {

        EasyHttp.post(PracticeUrl.GET_IS_VOLUNTEER)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phone", phone)
                .execute(new SimpleCallBack<PracticeIsVolunteerBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setIsVolunteer(null);
                    }

                    @Override
                    public void onSuccess(PracticeIsVolunteerBean practiceIsVolunteerBean) {
                        mPresenter.setIsVolunteer(practiceIsVolunteerBean);
                    }
                });
    }
}
